What is requires-port?
The `requires-port` package is a simple utility used to determine if a default port is required for a given protocol. It's particularly useful when working with URLs or networking in Node.js applications, where you might need to append a port to a hostname based on the protocol being used. This package helps in making decisions about whether to include the port in a URL string.
What are requires-port's main functionalities?
Checking if a port is required for a protocol
This feature allows you to check if a specific port is typically required for a given protocol. For standard protocols like HTTP (80) and HTTPS (443), it returns `false` indicating that the port does not need to be explicitly mentioned in a URL since it's the default. For non-standard ports or protocols, it returns `true`, suggesting that the port should be specified. The code sample demonstrates how to use `requires-port` to check for common and custom protocols.
"use strict";\nconst requiresPort = require('requires-port');\n\n// Example for HTTP\nconsole.log(requiresPort(80, 'http')); // false\n\n// Example for HTTPS\nconsole.log(requiresPort(443, 'https')); // false\n\n// Example for a custom protocol\nconsole.log(requiresPort(8080, 'my-protocol')); // true"
Other packages similar to requires-port
is-port-reachable
While `requires-port` helps determine if a port is required for a protocol, `is-port-reachable` checks if a port is reachable on a host. It's more about network accessibility rather than protocol requirements, making it useful for testing if services are up and accessible.
get-port
The `get-port` package is designed to find an available port on the system. Unlike `requires-port`, which is about understanding protocol defaults, `get-port` is useful for dynamically selecting an open port to avoid conflicts, especially during development or when launching new server instances.
requires-port
The module name says it all, check if a protocol requires a given port.
Installation
This module is intended to be used with browserify or Node.js and is distributed
in the public npm registry. To install it simply run the following command from
your CLI:
npm install --save requires-port
Usage
The module exports it self as function and requires 2 arguments:
- The port number, can be a string or number.
- Protocol, can be
http
, http:
or even https://yomoma.com
. We just split
it at :
and use the first result. We currently accept the following
protocols:
http
https
ws
wss
ftp
gopher
file
It returns a boolean that indicates if protocol requires this port to be added
to your URL.
'use strict';
var required = require('requires-port');
console.log(required('8080', 'http'))
console.log(required('80', 'http'))
License
MIT